﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace HeathCareSystem
{
    public class ChiTietDonThuocDAO
    {
        DataProvider dp = new DataProvider();

        public bool ThemChiTietDonThuocMoi(ChiTietDonThuocDTO ctdt)
        {
            string strSQL = string.Format("INSERT INTO ChiTietDonThuoc (MaDonThuoc, MaThuoc, SoLuong, ChiDinh1, ChiDinh2, ChiDinhThem) "+
                                          " VALUES ({0}, {1}, {2}, N'{3}', N'{4}', N'{5}') ",
                                            ctdt.maDonThuoc, ctdt.maThuoc, ctdt.soLuong, ctdt.chiDinh1, ctdt.chiDinh2, ctdt.chiDinhThem);
            return dp.ExecuteNonQuery(strSQL);
        }

        public bool XoaChiTietDonThuoc(int maChiTietDonThuoc)
        {
            string strSQL = string.Format("DELETE FROM ChiTietDonThuoc WHERE (MaChiTietDonThuoc = {0})", maChiTietDonThuoc);
            return dp.ExecuteNonQuery(strSQL);
        }

        public List<ChiTietDonThuocDTO> DanhSachDonThuoc(int maChiTietBenhAn, string ngayLap)
        {
            List<ChiTietDonThuocDTO> lstKetQua = new List<ChiTietDonThuocDTO>();

            ChiTietDonThuocDTO ctDonThuoc;
            DataTable dt = new DataTable();
            string strSQL = string.Format("SELECT ChiTietDonThuoc.MaChiTietDonThuoc, ChiTietDonThuoc.MaDonThuoc, Thuoc.TenThuoc, ChiTietDonThuoc.MaThuoc, ChiTietDonThuoc.SoLuong, ChiTietDonThuoc.ChiDinh1, ChiTietDonThuoc.ChiDinh2, " + 
                                          " ChiTietDonThuoc.ChiDinhThem " +
                                          " FROM ChiTietDonThuoc INNER JOIN DonThuoc ON ChiTietDonThuoc.MaDonThuoc = DonThuoc.MaDonThuoc INNER JOIN "+
                                          " Thuoc ON ChiTietDonThuoc.MaThuoc = Thuoc.MaThuoc "+
                                          " WHERE (DonThuoc.MaChiTietBenhAn = {0}) AND (DonThuoc.NgayLap = CONVERT(DATETIME, '{1}', 102))", maChiTietBenhAn, ngayLap);
            dt = dp.ExecuteQuery(strSQL);
            try
            {
                foreach (DataRow dr in dt.Rows)
                {
                    ctDonThuoc = new ChiTietDonThuocDTO();
                    ctDonThuoc.maChiTietDonThuoc = Int32.Parse(dr["MaChiTietDonThuoc"].ToString());
                    ctDonThuoc.maDonThuoc = Int32.Parse(dr["MaDonThuoc"].ToString());
                    ctDonThuoc.tenThuoc = dr["TenThuoc"].ToString();
                    ctDonThuoc.maThuoc = Int32.Parse(dr["MaThuoc"].ToString());
                    ctDonThuoc.soLuong = Int32.Parse(dr["SoLuong"].ToString());
                    ctDonThuoc.chiDinh1 = dr["ChiDinh1"].ToString();
                    ctDonThuoc.chiDinh2 = dr["ChiDinh2"].ToString();
                    ctDonThuoc.chiDinhThem = dr["ChiDinhThem"].ToString();

                    lstKetQua.Add(ctDonThuoc);
                }
            }
            catch (Exception ex)
            {
                lstKetQua = null;
                Console.WriteLine("Error accessing the database: {0}", ex.Message);
            }
            return lstKetQua;
        }
    }
}